angular.module('eYe')
	.controller('QcmController', function($scope, $routeParams, $location, Popup, DataBase, safeApply, Connexion) {

		var idQcm = $routeParams.qcmId;
		var tabQcm = {};
		 
		$scope.affichageSolution = false;
		$scope.score = '';
		$scope.message_evaluation = '';
		 
		var tabEvaluations = [];

		$.when(
			getValueQcmsAndQuestions(DataBase.BD_Themes_Qcms,'id',idQcm),
			DataBase.BD_Themes_Qcms.Evaluations.toArray()
		).then(function(qcm,evaluations){
			$scope.qcm = qcm[0];
			tabEvaluations = evaluations;
			safeApply($scope); 
			Connexion.cacherCacheConnexion();
		});
				 
		 $scope.selectionChoix = function(idQuestion,typeQuestion,idReponse) {
			$('#question_'+idQuestion+' h3').removeClass("auncune_reponse");
			
			if($('#choix_'+idReponse+' input').attr('checked') == 'checked'){
				$('#choix_'+idReponse+' label').removeClass('reponse_coche');
				$('#choix_'+idReponse+' input').removeAttr('checked');
			}
			else{				
			
				if(typeQuestion == 1){
					$('#question_'+idQuestion+' label').removeClass("reponse_coche");
					$('#question_'+idQuestion+' input').removeAttr('checked');					
				}
			
				$('#choix_'+idReponse+' label').addClass('reponse_coche');	
				$('#choix_'+idReponse+' input').attr('checked','checked');
			}
		}
		
		$scope.afficherIndication = function(id){
			var bloc = $("#question_"+id).find(".indication_question");
			var caret_down = $("#question_"+id).find(".lien_indication_question").find(".caret_down");
			var caret_up = $("#question_"+id).find(".lien_indication_question").find(".caret_up");

			if(bloc.css('display') == 'none'){
				bloc.show();
				caret_down.hide();
				caret_up.show();
			}
			else{
				bloc.hide();
				caret_down.show();
				caret_up.hide();
			}
		}

		$scope.afficherSolution = function(id){
			var bloc = $("#question_"+id).find(".solution_question");
			var caret_down = $("#question_"+id).find(".lien_solution_question").find(".caret_down");
			var caret_up = $("#question_"+id).find(".lien_solution_question").find(".caret_up");

			if(bloc.css('display') == 'none'){
				bloc.show();
				caret_down.hide();
				caret_up.show();
			}
			else{
				bloc.hide();
				caret_down.show();
				caret_up.hide();
			}
		}			 
	
		 $scope.verifierQcm = function() {
			
			var nbBonsChoix = 0;
			var nbBonnesReponses = 0;
			var nbQuestionsSansReponse = 0;
			
			var listeBonnesReponses = '';
			var listeMauvaisesReponses = '';
			var listeQuestionsNonRepondues = '';
			
			var qcmActuel = $scope.qcm;

			$('#affichage_qcm .affichage_question h3').removeClass("auncune_reponse");
			
			$.each(qcmActuel.questions,function(index, value) {	
			
				var nbReponsesCochees = 0;
				var nbBonsChoixRep = 0;

				$.each(value.reponses,function(index, value) {	
					if(value.bonne){						
						if($('#choix_'+value.id+' input').attr('checked') == 'checked'){
							nbBonsChoixRep ++;
							nbReponsesCochees ++;
						}
							
						nbBonnesReponses ++;
							
						if(listeBonnesReponses != ''){
							listeBonnesReponses += ',';
						}						
							
						listeBonnesReponses += '#choix_'+value.id;
							
					}
					else if($('#choix_'+value.id+' input').attr('checked') == 'checked'){
						
						if(listeMauvaisesReponses != ''){
							listeMauvaisesReponses += ',';
						}						
							
						listeMauvaisesReponses += '#choix_'+value.id;						
						nbReponsesCochees ++;
					}					
					
				});

				if((value.type == 2) && (nbReponsesCochees != nbBonsChoixRep)){
					nbBonsChoixRep = 0;
				}

				nbBonsChoix = nbBonsChoix + nbBonsChoixRep;
				
				if(nbReponsesCochees == 0){
					
					if(listeQuestionsNonRepondues != ''){
						listeQuestionsNonRepondues += ',';
					}						
						
					listeQuestionsNonRepondues += '#question_'+value.id+' h3';					
					nbQuestionsSansReponse++;
				}
			});
				
			function afficherResultats(reponse){
			
				$('html,body').animate({scrollTop: 0}, 'slow');
				 $scope.affichageSolution = true;

				if(reponse == 1){			
					if(listeBonnesReponses != ''){
						$(listeBonnesReponses).find('table td:nth-child(3)').html('<div class="bonne_reponse"></div>');			
					}
					
					if(listeMauvaisesReponses != ''){					
						$(listeMauvaisesReponses).find('table td:nth-child(3)').html('<div class="mauvaise_reponse"></div>');
					}
					
					var resultat = nbBonsChoix+'/'+nbBonnesReponses;
					$scope.score = 'Résultat : '+resultat;		
					
					var pourcentage = Math.round(((nbBonsChoix/nbBonnesReponses)*100)*100)/100;
					
					var msg = '';
					tabEvaluations.forEach(function(evaluation){
						if(evaluation.tranche_inf <= pourcentage && evaluation.tranche_sup >= pourcentage){
							msg = evaluation.description;
						}
					});
					$scope.message_evaluation = msg;


					if(!Connexion.invite){
						var dateActuelle = new Date();
						DataBase.addValueTable(DataBase.BD_Donnees_Utilisateur,DataBase.BD_Donnees_Utilisateur.Statistiques,new Statistique({id_stat:-1,note:resultat,date:dateActuelle.getTime(),qcm_id:qcmActuel.id,utilisateur_id:utilisateurActuel.id}),false);
					}
					
					$('#lien_check_qcm').hide();
					
				}
				else if(reponse == 0){
					$(listeQuestionsNonRepondues).addClass("auncune_reponse");
				}
			}			
				
				
			if(nbQuestionsSansReponse > 0){
				var config = {typePopup:'autre',message:'Vous n\'avez pas répondu à '+nbQuestionsSansReponse+' question(s).<br><br>Voulez-vous valider le QCM ?',fonctionReponse:afficherResultats,btn_fermer:false,btn_oui_non:true};
				Popup.afficherPopup(config);
			}		
			else{
				afficherResultats(1);
			}
				     			
		 };
		 
		 $scope.signalerProbleme = function(){
			$location.path('/contact/3/qcm/'+idQcm);
		 };
	});